<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\View;
// use think\Request;
 use app\admin\controller\Base;
class Type extends Base 
{

    public function showPage($page,$totalPage,$sep=" "){
        $url = $_SERVER ['PHP_SELF'];           //获取当前路径

        $index = ($page == 1) ? "首页" : "<a href='{$url}?page=1'>首页</a>";
        $last = ($page == $totalPage) ? "尾页" : "<a href='{$url}?page={$totalPage}'>尾页</a>";

        $prevPage=($page>=1)?$page-1:1;
        $nextPage=($page>=$totalPage)?$totalPage:$page+1;
        $prev = ($page == 1) ? "上一页" : "<a href='{$url}?page={$prevPage}'>上一页</a>";
        $next = ($page == $totalPage) ? "下一页" : "<a href='{$url}?page={$nextPage}'>下一页</a>";

        $str = "总共{$totalPage}页/当前是第{$page}页";

        $old_page = $page;
        if($old_page<=6){

            $old_page = 6;
        }else{

            $old_page = $page;
        }

        $p="";
        for($i = $old_page-5; $i <= $old_page+5; $i ++) {
            //当前页无连接
            if ($page == $i) {
                $p .= "[{$i}]";
            } else {
                $p .= "<a href='{$url}?page={$i}'>[{$i}]</a>";
            }
        }
        
        $pageStr=$str.$sep . $index .$sep. $prev.$sep. $p.$sep .$next.$sep . $last;
        return $pageStr;    //返回分页字符串
    }

    public function goods_type(){

    	header("content-type:text/html;charset=utf-8");

        $where = array();

        $cat_name = input("get.cat_name");
        $attr_group = input("get.attr_group");

        if(!empty($cat_name)){

            $where['cat_name'] = array("like","%$cat_name%");
        }
        if(!empty($attr_group)){

            $where['attr_group'] = array("like","%$attr_group%");
        }

        $data = Db("goods_type")->where($where)->select();

        $page = input("get.page")?input("get.page"):1;
        $pageSize = 10;
        $offset = ($page-1)*$pageSize;
        $totalRows = count($data);
        $totalPage = ceil($totalRows/$pageSize);

        $data['datas'] = Db("goods_type")->where($where)->limit($offset,$pageSize)->select();
        $data['pagess'] = $this->showPage($page,$totalPage,$sep="");

        if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){

            echo json_encode($data);
        }else{

            $this->assign("data",$data);
            return $this->fetch();
        }	 
    }

    public function goods_typetwo()
    {
        return $this->fetch();
    }

    public function addGoodsType()
    {

    	$cat_name = input('post.cat_name');
    	$enabled = input('post.enabled');
    	$attr_group = input('post.attr_group');
    	$data = input('post.');
    	
    	$res = Db("goods_type")->insert($data);
    	if($res){

    		$this->success('添加成功',"Type/goods_type");
    	}else{

    		$this->error('添加失败',"Type/goods_typetwo");
    	}
    }

    public function del(){

    	$cat_id = input("get.cat_id");
    	$res = Db("goods_type")->delete($cat_id);
    	if($res){

    		$this->success('删除成功',"Type/goods_type");
    	}else{

    		$this->error('删除失败',"Type/goods_type");
    	}
    }

    public function changeWords(){

        $cat_id = input("get.cat_id");
        $cat_name = input("get.cat_name");

        $res = Db('goods_type')->where("cat_id",$cat_id)->update(["cat_name"=>$cat_name]);

        if($res){

            $arr['status'] = 0;
            $arr['data'] = '';
            $arr['msg'] = "成功";
        }else{

            $arr['status'] = 1;
            $arr['data'] = '';
            $arr['msg'] = "失败";
        }

        echo json_encode($arr);
    }

    public function changeStatus(){

        $cat_id = input('get.cat_id');
        $enabled = input('get.enabled');

        if($enabled==1){

            $res = Db("goods_type")->where("cat_id",$cat_id)->update(["enabled"=>'0']);
        }else{

            $res = Db("goods_type")->where("cat_id",$cat_id)->update(["enabled"=>'1']);
        }

        if($res){

            $arr['status'] = 0;
            $arr['data'] = '';
            $arr['msg'] = '成功';
        }else{

            $arr['status'] = 1;
            $arr['data'] = '';
            $arr['msg'] = '失败';
        }

        echo json_encode($arr);
    }
}